/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package core;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import model.Dapcomposition;
import model.DapcompositionId;
import model.Dapstructure;
import model.Dapversion;
import model.Product;
import model.Seller;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import util.IgsiHibernateUtil;

/**
 *
 * @author phinguyen
 */
public class Core {

    private static Session session;

    public Core() {
        session = IgsiHibernateUtil.getSessionFactory().openSession();

    }

    /**
     * sauvegarde 
     * @param product
     */
    public void saveProduct(Product product) {
        Transaction tr = session.beginTransaction();
        session.saveOrUpdate(product);
        tr.commit();
    }

    /**
     * return all dapversion register in the database
     * @return 
     */
    public List<Dapversion> getAllDapversion() {
        Query query = this.session.getNamedQuery("HQL_ALL_DAPVERSION");
        List<Dapversion> listAllDapversion = query.list();
        return listAllDapversion;
    }

    /**
     * return all sellers register in the database
     * @return 
     */
    public List<Dapversion> getAllSeller() {
        Query query = this.session.getNamedQuery("HQL_ALL_SELLER");
        List<Dapversion> listAllSeller = query.list();
        return listAllSeller;
    }

    /**
     * return all Dapversion of a seller
     *
     * @param seller
     * @return
     */
    public List<Dapversion> getAllDapversionForSeller(Seller seller) {
        HashSet<Dapversion> answer = new HashSet();
        for (Dapversion dapversion : getAllDapversion()) {
            if (dapversion.getSid() == (seller.getSid())) {
                answer.add(dapversion);
            }
        }
        return new ArrayList(answer);
    }

    /**
     * return an object of seller with his identification number
     *
     * @param id
     * @return
     */
    public Seller getSellerById(int id) {
        return (Seller) session.get(Seller.class, id);
    }
    
     public void saveDap(Dapstructure dap) {
        Transaction tr = session.beginTransaction();
        session.saveOrUpdate(dap);
        tr.commit();
        
    }
     public void addsteptoDap(int order,int stepReturnID, int stepID,int dsid){
                  Transaction tr = session.beginTransaction();

         DapcompositionId dapcompositionid;
         dapcompositionid= new DapcompositionId(dsid, stepID);
         Dapcomposition dapcomposition;
         dapcomposition= new Dapcomposition(dapcompositionid, order, stepReturnID);
                session.saveOrUpdate(dapcomposition);
         tr.commit();
         
     }
     public Dapstructure getDapstructure(int stepId){
         
         return (Dapstructure) session.get(Dapcomposition.class, stepId);
     }
}
